package no.ntnu.fp.model;

import java.awt.LayoutManager;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import no.ntnu.fp.client.ClientConnectionHandler;
import no.ntnu.fp.client.ClientModelHandler;

public class Message extends AbstractModel {
	public enum MessageType {
		INVITE, DEINVITE, MEETING_CHANGED, MEETING_CANCELED
	}
	
	public Message() {
		
	}

	private static Map<Integer, Message> instances = new HashMap<Integer, Message>();
	public void register(int id) {
		instances.put(id, this);
	}
	public static Message get(int id) {
		MessageType.values();
		return instances.get(id);
	}
	
	public Message(Appointment appointment, Person to, MessageType messageType) {
		this.messageType = messageType;
		this.id = requestId();
		this.to = to.id;
		
		register(id);
	}

	public MessageType messageType;
	public int appointment;
	public boolean active = true;
	public int to;
	
	public MessageType getMessageType() {
		return messageType;
	}
	
	public static List<Message> all() {
		List<Message> all = new ArrayList<Message>();
		for (Map.Entry<Integer, Message> entry : instances.entrySet()) {
			Integer id = entry.getKey();
			if(id < 0) continue;
			Message message = entry.getValue();
			all.add(message);
		}
		return all;
	}
	
	public void setMessageType(MessageType messageType) {
		this.messageType = messageType;
	}

	public Appointment getAppointment() {
		return Appointment.get(appointment);
	}

	public void setAppointment(Appointment appointment) {
		this.appointment = appointment.getId();
	}

	@Override
	public Map<String, Object> pack() {
		Map<String, Object> m = createAttributeMap();
		m.put("appointment", appointment);
		m.put("active", active);
		m.put("to", to);
		m.put("messageType", messageType.ordinal());
		return m;
	}

	@Override
	public void writeAttribute(String attribute, Object value) {
		if (attribute.equals("appointment")) {
			appointment = Integer.parseInt((String) value);
		} else if (attribute.equals("active")) {
			active = Boolean.parseBoolean((String) value);
		} else if (attribute.equals("messageType")) {
			messageType = MessageType.values()[Integer.parseInt((String) value)];
		} else if (attribute.equals("to")) {
			to = Integer.parseInt((String) value);
		}
	}

	public void messageViewed() {
		setActive(false);
	}

	public void setMessageActiveState(boolean activeState) {
		setActive(activeState);
		ClientModelHandler.updateModel(this);
	}
/*
	public static Message example() {
		//return new Message(Appointment.get(1), Person.get(1), MessageType.INVITE);
		return new Message(Appointment.example(), Person.exampleCurrentUser(), MessageType.INVITE );
	}
*/
	public static List<Message> getAllTo(Person person) {
		List<Message> list = new ArrayList<Message>();
		for (Integer id : instances.keySet()) {
			Message m = instances.get(id);
			if(m == null){
				System.out.println("m");
			}
			if(person == null){
				System.out.println("p");
			}
			if (m.to == person.getId()) {
				list.add(m);
			}
		}
		return list;
	}

	public void setTo(Person to) {
		this.to = to.getId();
	}

	public Person getTo() {
		return Person.get(to);
	}

	public void setActive(boolean active) {
		this.active = active;
	}

	public boolean isActive() {
		return active;
	}

}
